//
//  DailyDietView.m
//  SHHCPhone
//
//  Created by gfsh on 14-6-20.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "DailyDietView.h"
#import "DailyDietInfo.h"

@implementation DailyDietView
{
    UIButton *leftBtn;
    UIButton *rightBtn;
    UITableView *tv;
    UIButton *changeFoodBtn;
    UIView *line;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        line = [[UIView alloc]initWithFrame:CGRectMake(27+3, 9+27+17+9, 2, self.frame.size.height-(9+27+17))];
        line.backgroundColor = GET_COLOR(@"f6f6f6");
        [self addSubview:line];
        
        //菜品推荐
        leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        leftBtn.frame = CGRectMake(9, 9, (self.frame.size.width-18)/2, 27);
        [leftBtn setTitle:@"菜品推荐" forState:UIControlStateNormal];
        [leftBtn setTitleColor:GET_COLOR(@"fe8cb6") forState:UIControlStateNormal];
        [leftBtn setTitleColor:GET_COLOR(@"ffffff") forState:UIControlStateSelected];
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"标签-左-未选中"] forState:UIControlStateNormal];
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"标签-左-未选中"] forState:UIControlStateHighlighted];
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"标签-左-选中"] forState:UIControlStateSelected];
        [leftBtn addTarget:self action:@selector(tagLeftBtnClick) forControlEvents:UIControlEventTouchUpInside];
//        leftBtn.selected = YES;
        //食材推荐
        rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        rightBtn.frame = CGRectMake(leftBtn.frame.origin.x+leftBtn.frame.size.width, 9, (self.frame.size.width-18)/2, 27);
        [rightBtn setTitle:@"食材推荐" forState:UIControlStateNormal];
        [rightBtn setTitleColor:GET_COLOR(@"fe8cb6") forState:UIControlStateNormal];
        [rightBtn setTitleColor:GET_COLOR(@"ffffff") forState:UIControlStateSelected];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"标签-右-未选中"] forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"标签-右-未选中"] forState:UIControlStateHighlighted];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"标签-右-选中"] forState:UIControlStateSelected];
        [rightBtn addTarget:self action:@selector(tagRightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:leftBtn];
        [self addSubview:rightBtn];
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setFrame:leftBtn.frame];
        [button1 setShowsTouchWhenHighlighted:YES];
        [self addSubview:button1];
        [button1 addTarget:self action:@selector(tagLeftBtnClick) forControlEvents:UIControlEventTouchUpInside];
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button2 setFrame:rightBtn.frame];
        [self addSubview:button2];
        [button2 setShowsTouchWhenHighlighted:YES];
        [button2 addTarget:self action:@selector(tagRightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        //食谱列表
        tv = [[UITableView alloc]initWithFrame:CGRectMake(9, 9+27+10, self.frame.size.width-18, self.frame.size.height-(9+27+10)-(20+37+20)) style:UITableViewStyleGrouped];
        tv.separatorStyle = UITableViewCellSeparatorStyleNone;
        tv.separatorColor = [UIColor clearColor];
        tv.showsVerticalScrollIndicator = NO;
        tv.backgroundColor = [UIColor clearColor];
        tv.backgroundView = nil;
        tv.dataSource = self;
        tv.delegate = self;
        
        [self addSubview:tv];
        
        //换一换菜品
        UIView *changeBg = [[UIView alloc]initWithFrame:CGRectMake(50, self.frame.size.height-(20+37+20), self.frame.size.width-50, 20+37+20)];
        changeBg.backgroundColor = [UIColor whiteColor];
        [self addSubview:changeBg];
        changeFoodBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        changeFoodBtn.frame = CGRectMake(changeBg.frame.size.width/2-135/2-50/2, 20, 135, 37);
        [changeFoodBtn setBackgroundImage:[UIImage imageNamed:@"按钮"] forState:UIControlStateNormal];
        [changeFoodBtn setBackgroundImage:[UIImage imageNamed:@"按钮-点击"] forState:UIControlStateHighlighted];
        //[changeFoodBtn setTitle:@"换一换菜品" forState:UIControlStateNormal];
        [changeFoodBtn setTitleColor:GET_COLOR(@"ffffff") forState:UIControlStateNormal];
        [changeFoodBtn addTarget:self action:@selector(changeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [changeBg addSubview:changeFoodBtn];
    
    }
    return self;
}

- (void)setData:(NSArray *)data
{
    if (!data) {
        line.hidden = YES;
    }else{
        line.hidden = NO;
    }
    
    NSMutableArray *arr = [NSMutableArray array];
    
    for (DailyDietInfo *ddi in data) {
        if (ddi.dietInfos.count) {
            [arr addObject:ddi];
        }
    }
    
    _data = arr;
    [tv setContentOffset:CGPointMake(0, 0)];
    [tv reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DailyDietInfo *ddi = self.data[section];
    if (ddi.isFold) {
        return 2;
    }
    return ddi.dietInfos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"dailyDietViewCellIdentifier";
    DailyDietViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[DailyDietViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.delegate = self;
    DailyDietInfo *ddi = self.data[indexPath.section];
    if (indexPath.row == 0) {
        cell.mealBtn.hidden = NO;
        if (ddi.isFold) {
            cell.mealBtn.backgroundColor = GET_COLOR(@"f6f6f6");
            cell.mealBtn.selected = NO;
        }else{
            cell.mealBtn.backgroundColor = GET_COLOR(@"ff8cb7");
            cell.mealBtn.selected = YES;
        }
        [cell hideShowOther:YES];
        [cell.mealBtn setTitle:ddi.mealName forState:UIControlStateNormal];
        [cell.mealBtn setTitle:ddi.mealName forState:UIControlStateSelected];
        UIImage *img = [UIImage imageNamed:@"每日食谱-帖子背景"];
        cell.bg.image = [img stretchableImageWithLeftCapWidth:10 topCapHeight:20];
    }else{
        cell.mealBtn.hidden = YES;
        [cell.mealBtn setTitle:@"" forState:UIControlStateNormal];
        [cell.mealBtn setTitle:@"" forState:UIControlStateSelected];
    }
    if (ddi.isFold) {
        if (indexPath.row == 0) {
            DietInfo *di = ddi.dietInfos[indexPath.row];
            cell.foodLab.text = di.foodName;
            cell.detailLab.text = di.foodDetail;
            if (di.picPath) {
                [cell.mealPic setImageWithURL:[NSURL URLWithString:di.picPath]];
            }else{
                cell.mealPic.image = [UIImage imageNamed:@"默认图片icon"];
            }
        }
        if (indexPath.row == 1) {
            [cell hideShowOther:NO];
            cell.showOther.text = leftBtn.selected?@"查看其他菜品":@"查看其他食材";
            UIImage *img = [UIImage imageNamed:@"每日食谱-帖子背景1"];
            cell.bg.image = [img stretchableImageWithLeftCapWidth:10 topCapHeight:20];
        }
        
    }else{
        DietInfo *di = ddi.dietInfos[indexPath.row];
        cell.foodLab.text = di.foodName;
        cell.detailLab.text = di.foodDetail;
        if (di.picPath) {
            [cell.mealPic setImageWithURL:[NSURL URLWithString:di.picPath]];
        }else{
            cell.mealPic.image = [UIImage imageNamed:@"默认图片icon"];
        }
        if (indexPath.row>0) {
            [cell hideShowOther:YES];
            UIImage *img = [UIImage imageNamed:@"每日食谱-帖子背景1"];
            cell.bg.image = [img stretchableImageWithLeftCapWidth:10 topCapHeight:20];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getRowHeightAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5.f;
}

- (CGFloat)getRowHeightAtIndexPath:(NSIndexPath*)indexPath
{
    return 50.f;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

#pragma mark refresh DailyDietViewCell
- (void)refreshCell:(NSIndexPath*)ip
{
    [tv reloadRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark DailyDietViewCell delegate

- (void)refreshBtnClick:(id)obj
{
    DailyDietViewCell *cell = (DailyDietViewCell*)obj;
    NSIndexPath *ip = [tv indexPathForCell:cell];

    if ([self.delegate respondsToSelector:@selector(refreshFood:)]) {
        [self.delegate performSelector:@selector(refreshFood:) withObject:/*ddi.dietInfos[ip.row]*/ip];
    }
}

- (void)cellItemSelected:(id)obj
{
    DailyDietViewCell *ddvc = (DailyDietViewCell*)obj;
    if (ddvc) {        
        NSIndexPath *ip = [tv indexPathForCell:ddvc];
        if (ddvc.showOther.hidden) {
            DailyDietInfo *ddi = [self.data objectAtIndex:ip.section];
            if ([self.delegate respondsToSelector:@selector(cellClick:)]) {
                [self.delegate performSelector:@selector(cellClick:) withObject:ddi.dietInfos[ip.row]];
            }
        }else{
            for (DailyDietInfo *ddi in self.data) {
                NSInteger index = [self.data indexOfObject:ddi];
                if (index == ip.section) {
                    ddi.isFold = NO;
                }else{
                    ddi.isFold = YES;
                }
            }

            [tv reloadData];
            
            dispatch_async(dispatch_get_main_queue(),^{
                NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:ip.section];
                [tv scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
            });
        }
    }
}

- (void)cellMealBtnClick:(id)obj
{
    DailyDietViewCell *ddvc = (DailyDietViewCell*)obj;
    if (ddvc) {
        NSIndexPath *ip = [tv indexPathForCell:ddvc];
        for (DailyDietInfo *ddi in self.data) {
            NSInteger index = [self.data indexOfObject:ddi];
            if (index == ip.section) {
                ddi.isFold = NO;
            }else{
                ddi.isFold = YES;
            }
        }
        [tv reloadData];
    }
    
}

#pragma mark leftBtn、rightBtn、changeBtn 事件

- (void)tagLeftBtnClick
{
    if (!leftBtn.selected) {
        if ([self.delegate respondsToSelector:@selector(leftBtnClick)]) {
            leftBtn.selected = YES;
            rightBtn.selected = NO;
            [changeFoodBtn setTitle:@"换一换菜品" forState:UIControlStateNormal];
            [self.delegate performSelector:@selector(leftBtnClick)];
        }
    }
}

- (void)tagRightBtnClick
{
    if (!rightBtn.selected) {
        if ([self.delegate respondsToSelector:@selector(rightBtnClick)]) {
            leftBtn.selected = NO;
            rightBtn.selected = YES;
            [changeFoodBtn setTitle:@"换一换食材" forState:UIControlStateNormal];
            [self.delegate performSelector:@selector(rightBtnClick)];
        }
    }
}

- (void)changeBtnClick
{
    if ([self.delegate respondsToSelector:@selector(changeFoodBtnClick)]) {
        [self.delegate performSelector:@selector(changeFoodBtnClick)];
    }
}

@end
